// pages/playsong/playsong.js
import PubSub from 'pubsub-js'
import moment from 'moment'
import request from '../../utils/requests'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isPlay:false,
        musicId:'',
        music: {},
        song:[],
        currentTime : '00:00', //实时时间   
        durationTime : '00:00', //总时长
        currentWidth : '',
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var connst = getApp();
        this.setData({
            musicId:options.musicId,
            song:[]
        })
        this.getSongDetail();
        this.getMusic();
        this.backgroundaudio = wx.getBackgroundAudioManager();
        // if(connst.globalMusic.isMusicPlay && connst.globalMusic.musicId === musicId){
        //     this.setData({
        //         isPlay:true
        //     })
        // }
        this.backgroundaudio.onPlay(() => {
            this.setData({
                isPlay:true
            })
            connst.globalMusic.isMusicPlay = true;
            connst.globalMusic.musicId=this.data.musicId;
        });
        this.backgroundaudio.onPause(() => {
            this.setData({
                isPlay:false
            })
            connst.globalMusic.isMusicPlay = false;
        });
        this.backgroundaudio.onStop(() => {
            this.setData({
                isPlay:false
            })
            connst.globalMusic.isMusicPlay = false;
        });
        this.backgroundaudio.onTimeUpdate(() => {
            let currentTime = moment(this.backgroundaudio.currentTime * 1000).format('mm:ss')
            //console.log(currentTime);
            let currentWidth = this.backgroundaudio.currentTime/this.backgroundaudio.duration * 450; 
            this.setData({
                currentTime,
                currentWidth
            })
        });
        this.backgroundaudio.onEnded(() => {
            PubSub.publish('switchsong','next');
            this.setData({
                currentWidth:0,
                currentTime:'00:00'
            })
        });
    },

    handleSwitch(event){
        let type = event.currentTarget.id;
        PubSub.subscribe('setMusicID',(msg,data) => {
            if(data){
                this.musicControl(false);
                this.setData({
                    musicId:data
                })
                this.getSongDetail();
                this.getMusic();
                if(this.backgroundaudio!=null){
                    this.musicControl(true);
                }
            }
            PubSub.unsubscribe('setMusicID');
        });
        PubSub.publish('switchsong', type);
    },

    handleMusicPlay(){
        let play = !this.data.isPlay;
        // this.setData({
        //     isPlay:play
        // })
        this.musicControl(play);
    },

    async getMusic(){
        let ids = this.data.musicId;
        let result = await request('/song/url',{id:ids});
        if(result.code === 200){
            this.setData({
                music:result.data[0]
            })
        }
    },

    musicControl(isPlay){
        if(isPlay){
            this.backgroundaudio.src = this.data.music.url;
            this.backgroundaudio.title = this.data.song.al.name;
            this.backgroundaudio.play();
        }else{
            this.backgroundaudio.pause();
        }
    },

    async getSongDetail(){
        let result = await request('/song/detail',{ids:this.data.musicId});
        //console.log(result);
        if(result.code === 200){
            //console.log(result);
            let durationTime = result.songs[0].dt;
            let tmp = moment(durationTime).format('mm:ss');
            this.setData({
                song:result.songs[0],
                durationTime:tmp,
                currentTime:'00:00'
            })
            wx.setNavigationBarTitle({
              title: this.data.song.al.name,
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})